package com.cskaoyan.app.controller;

import com.cskaoyan.app.bean.bo.adminbo.AdminAdminBo;
import com.cskaoyan.app.bean.bo.adminbo.AdminDelBo;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketAdmin;
import com.cskaoyan.app.exception.ValidationException;
import com.cskaoyan.app.service.AdminAdminService;
import com.cskaoyan.app.util.ValidationUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * @author kiggy
 * @since 2022/12/28 21:45
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {

    @Autowired
    AdminAdminService adminAdminService;

    @GetMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo){
        // 获得分页信息
        // PageHelper.startPage(1,20);
        PageInfo<MarketAdmin> adminPageInfo = adminAdminService.listPageAdmins(basePageInfo);

        CommonData<MarketAdmin> marketAdminCommonData = CommonData.data(adminPageInfo);

        return BaseRespVo.ok(marketAdminCommonData);

    }

    /**
     * TODO:添加时注意用户名不能重复
     * 如果用户名已存在，adminAdminService.createAdminData(createBo)回null
     * errno:200,用户名已存在
     * @param createBo
     * @param bindingResult
     * @return
     * @throws ValidationException
     */
    @PostMapping("create")
    public BaseRespVo create(@Validated @RequestBody AdminAdminBo createBo, BindingResult bindingResult,MultipartFile file) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        // 将文件下载到本地的方式
        // adminAdminService.createPicFile(request);
        // String url = adminAdminService.aliFileUpload(file);
        // createBo.setAvatar(url);
        // 获得marketAdmin
        List<MarketAdmin> marketAdmin = adminAdminService.createAdminData(createBo);
        if (marketAdmin==null){
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(200);
            baseRespVo.setErrmsg("用户名已存在");
            return baseRespVo;
        }
        return BaseRespVo.ok(marketAdmin.get(0));
    }

    /**
     * 更新管理员账号
     * 接收数据，传出改完的数据
     * @param adminBo
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody AdminAdminBo adminBo){

        MarketAdmin marketAdmin = adminAdminService.update(adminBo);
        return BaseRespVo.ok(marketAdmin);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody AdminDelBo adminDelBo){
        adminAdminService.delAdminData(adminDelBo);
        return new BaseRespVo().ok(null);
    }

}
